package com.example.demo.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.config.ResponseBean;
import com.example.demo.config.ResponseMessage;
import com.example.demo.dao.StudentDao;
import com.example.demo.entity.StudentEntity;
import com.example.demo.form.QueryStudentForm;
import com.example.demo.service.StudentService;
import com.example.demo.util.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author BleethNie
 * @version 1.0
 * @date 2020-01-08 17:48
 **/
@Service("StudentService")
public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    public ResponseBean listStudent() {
        List<StudentEntity> studentEntities = studentDao.listStudent();
        return new ResponseBean(200,"查询成功",studentEntities);
    }

    @Override
    public ResponseBean listStuInfo(QueryStudentForm form) {


        //参数校验
        ValidatorUtil.validateEntity(form);

        String stuNameFilter = form.getStuNameFilter();
        String stuAgeFilter = form.getStuAgeFilter();

        Page<StudentEntity> entityPage = new Page<>(form.getPage(), form.getLimit());
        IPage<StudentEntity> resultPage = studentDao.listStu(entityPage, stuNameFilter, stuAgeFilter);
        return new ResponseBean(200, "查询成功", resultPage);

    }

    @Override
    public ResponseMessage addStu() {
        StudentEntity studentEntity = new StudentEntity();
        studentEntity.setAge(20);
        studentEntity.setName("张三");
         this.save(studentEntity);
        return new ResponseMessage(200,"保存成功");
    }

    @Override
    public ResponseMessage editStuInfo(Integer id) {

        StudentEntity studentEntity = new StudentEntity();

        studentEntity.setName("哈哈");
        studentEntity.setAge(12);
        this.updateById(studentEntity);
        return new ResponseMessage(200,"修改成功");
    }


}
